package com.androsz.andslr.app.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.mtp.MtpDevice;
import android.text.TextUtils.TruncateAt;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.androsz.andslr.Extras;
import com.androsz.andslr.Intents;
import com.androsz.andslr.R;
import com.androsz.util.MathUtils;

public class PtpDeviceButton extends Button {

	int lastDrawableId;

	public PtpDeviceButton(Context context, final MtpDevice ptpDevice) {
		super(context);
		// this mess is why android uses xml to design layouts.
		// unfortunately, this was the best solution I could find!
		setBackgroundResource(android.R.color.transparent);

		setTextAppearance(context, R.style.NotebookButtonText);
		// for some reason these don't get set by the style...
		setSingleLine(true);
		setEllipsize(TruncateAt.END);

		// set the notebook drawable to normal initially
		final Drawable notebookDrawable = refreshDrawable(R.drawable.dslr_normal);

		setPadding(0, MathUtils.calculatePxFromDip(context, 32), 0, 0);
		try {
			setText(ptpDevice.getDeviceInfo().getModel());
		} catch (NullPointerException npe) {
			setText("Unknown Device");
		}

		final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				notebookDrawable.getIntrinsicWidth(),
				ViewGroup.LayoutParams.WRAP_CONTENT);
		lp.gravity = Gravity.CENTER;
		setLayoutParams(lp);

		setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				open(ptpDevice.getDeviceId());
			}
		});
	}

	@Override
	public void onFocusChanged(boolean focused, int direction,
			Rect previouslyFocusedRect) {
		super.onFocusChanged(focused, direction, previouslyFocusedRect);
		int drawableId = R.drawable.dslr_normal;
		if (focused) {
			drawableId = R.drawable.dslr_focused;
		}
		refreshDrawable(drawableId);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		final int action = event.getAction();

		if (action == MotionEvent.ACTION_UP
				|| action == MotionEvent.ACTION_CANCEL) {
			post(new refreshDrawableRunnable(R.drawable.dslr_normal));
		} else {
			post(new refreshDrawableRunnable(R.drawable.dslr_pressed));
		}

		return super.onTouchEvent(event);
	}

	public void open(int deviceId) {
		final Context c = getContext();
		c.sendBroadcast(new Intent(Intents.OPEN_PTP_DEVICE).putExtra(
				Extras.PTP_DEVICE_ID, deviceId));
	}

	// this should be post'd so that it runs on the UI thread.
	private final class refreshDrawableRunnable implements Runnable {
		final int drawableId;

		refreshDrawableRunnable(int drawableId) {
			this.drawableId = drawableId;
		}

		@Override
		public void run() {
			refreshDrawable(drawableId);
		}
	}

	private Drawable refreshDrawable(int drawableId) {

		if (drawableId != lastDrawableId) {
			// need to use decodeResource and then create a new BitmapDrawable,
			// otherwise the color filter will be applied to all NotebookButtons
			final Bitmap b = BitmapFactory.decodeResource(getContext()
					.getResources(), drawableId);
			final Drawable notebookDrawable = new BitmapDrawable(b);
			// notebookDrawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
			setCompoundDrawablesWithIntrinsicBounds(null, notebookDrawable,
					null, null);
			lastDrawableId = drawableId;
			return notebookDrawable;
		}
		return null;
	}
}
